package q38_countAndSay;

public class Solution {
    public static void main(String[] args) {

    }

    public static String countAndSay(int n) {
        StringBuilder sb = new StringBuilder();
        sb.append("1");
        for (int i = 1; i < n; i++) {
            sb = count(sb);
        }
        return sb.toString();
    }

    private static StringBuilder count(StringBuilder sb) {
        StringBuilder sb2 = new StringBuilder();
        int count = 1;
        char cur = sb.charAt(0);
        for (int i = 1; i < sb.length(); i++) {
            if (sb.charAt(i) == cur) {
                count++;
            } else {
                sb2.append(count);
                sb2.append(cur);
                cur = sb.charAt(i);
                count = 1;
            }
        }
        sb2.append(count);
        sb2.append(cur);
        return sb2;
    }
}
